<?php
namespace App\V3\V3_2\Controllers;

use App\V3\V3_2\Services\DataFieldService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * 字段权限管理
 * AUTHOR  : zl
 * DateTime: 2022/08/17
 */
class DataFieldController extends BaseController
{
    /**
     * 权限字段列表
    */
    public function FieldList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
            'id' => 'required|integer',//用户id
        ], [
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
            'id.required' => '参数`用户id`缺失',
            'id.integer' => '参数`用户id`只能为整型',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        return response()->json(DataFieldService::getUserFieldPower($data,$this->getMainUserId()));

    }

    public function SetFieldPermission(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' => 'required|integer',//用户id
        ], [
            'id.required' => '参数`用户id`缺失',
            'id.integer' => '参数`用户id`只能为整型',
        ]);

        #dd(DataFieldService::distinguishPermission($data['id'],'cost'));

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        return response()->json(DataFieldService::setFieldPermission($data));

    }
}
